import { Modal } from "antd";
import axios from "axios";

const instance = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(config => {
    config.headers.Authorization = localStorage.adminToken;
    return config;
})

// 响应拦截器
instance.interceptors.response.use((res) => res.data, (error) => {
    if (error.status == 401) {
        Modal.warning({
            title: '警告',
            content: '登录已过期，请重新登录。',
            afterClose() {
                // localStorage.clear();
                localStorage.removeItem('adminToken');
                location.href = '/login';
            }
        })
    }
    return Promise.reject(error);
});

export default instance;